// 跳转自定义功能时, 需要判断是否在tabbar中, 如果在使用uni.reLaunch, 不在则使用uni.navigateTo

import { useTabbarStore } from '@/store'
import { selectMyUnitNumber } from '@/api/myUnit'

export async function modelNavigateTo(item) {

  if (!item || !item.url) {
    return
  }
  const data = item?.url

  switch (data.___value) {
    // 商品
    case 'goods':
      uni.navigateTo({
        url: `/pages/goods/product/detail?skuId=${data.id}&goodsId=${data.goodsId}`,
      });
      break;
    // 分类
    case 'category':
      // 针对于商家自己装修的分类单独做处理
      if (item?.other && item?.other.pageType && item?.other.pageType === 'STORE') {
        uni.navigateTo({
          url: `/pages/goods/store/store-goods?id=${item.url.id}&storeId=${item.other.id}&title=${item.url.___key}`
        })
      }
      else {
        uni.navigateTo({
          url: `/pages/goods/search/page?category=${data.id}`,
        });
      }

      break;
    // 文章
    case 'pages':

      console.log(data, item)
      uni.navigateTo({
        url: `/pages/p?id=${data.id}`,
      });
      break;
    // 店铺
    case 'shops':
      uni.navigateTo({
        url: ``,
      });
      break;
    // 微页面
    case 'special':
      console.log(data)
      uni.navigateTo({
        url: `/pages/topic?id=${data.id}`,
      });
      break;
    // 其他文章
    case 'other-article':
      console.log(data)
      uni.navigateTo({
        url: `/pages/funcArticle?contentId=${data.contentId}`,
      });
      break;
    // 其他文章
    case 'article-func':
      console.log(data)
      uni.navigateTo({
        url: `/pages/funcArticle?contentId=${data.contentId}`,
      });
      break;
  }

  if (data.___value === 'function-page' && data.functionType == 0) { // 图文功能
    uni.navigateTo({
      url: `/pages/funcPage?functionCode=${data.functionCode}&functionName=${data.functionName}`,
    });
  }
  if (data.___value === 'function-page' && data.functionType == 1) { // 非图文功能
    if (data.frontAddress === '/pages/exam/index') {
      console.log('前往考试')
      const unitRes = await selectMyUnitNumber()
      if (unitRes.data.result <= 0) {
        uni.showToast({
          title: '您未加入任何单位',
          icon: 'none',
          duration: 3000
        })
        return
      }
    }

    const tabbarStore = useTabbarStore()
    const tabbarList = tabbarStore.tabbarList.map((i) => i.linkUrl)
    const isTabbar = tabbarList.includes(data.frontAddress)
    if (data.frontAddress === '/pages/mall') {
      uni.reLaunch({
        url: `${data.frontAddress}?functionCode=${data.functionCode}&functionName=${data.functionName}`
      })
    } else if (isTabbar) {
      uni.reLaunch({
        url: data.frontAddress
      })
    } else {
      uni.navigateTo({
        url: `${data.frontAddress}?functionCode=${data.functionCode}&functionName=${data.functionName}`
      })
    }
  }

  if (data.___value === 'other') {
    switch (data.title) {
      case "首页":
        uni.switchTab({
          url: `/pages/index`,
        });
        break;
      case "购物车":
        uni.switchTab({
          url: `/pages/tabbar/cart`,
        });
        return;
      case "个人中心":
        uni.switchTab({
          url: `/pages/tabbar/mine`,
        });
        break;
      case "收藏商品":
        uni.navigateTo({
          url: `/pages/mine/collect`,
        });
        break;
      case "我的订单":
        uni.navigateTo({
          url: `/pages/mine/order/list`,
        });
        break;
      case "领券中心":
        uni.navigateTo({
          url: `/pages/promotions/coupon/center`,
        });
        break;
      case "签到":
        uni.navigateTo({
          url: ``,
        });
        break;
      case "秒杀频道":
        uni.navigateTo({
          url: `/pages/promotions/seckill/list`,
        });
        break;
      case "拼团频道":
        uni.navigateTo({
          url: `/pages/promotions/group/list`,
        });
        break;
      case "小程序直播":
        // #ifndef MP-WEIXIN
        uni.showToast({
          title: "除微信小程序，其他暂不支持小程序直播",
          icon: "none"
        })
        // #endif
        // #ifdef MP-WEIXIN
        uni.navigateTo({
          url: ``,
        });
        // #endif
        break;
      case "砍价":
        uni.navigateTo({
          url: `/pages/promotions/bargain/list`,
        });
        break;
      case "积分商城":
        uni.navigateTo({
          url: `/pages/promotions/point/store`,
        });
        break;
    }
  }
}
